#include "widget_ai_statistics.h"
#include "players.h"
#include "ai.h"



time_graph_widget::time_graph_widget( vector< double >* data, const string& title, double scale, int size, unsigned int pl_id, double mean, QWidget* parent ): Plot( title.c_str(), "Round number", "Time consumed (s)", false, parent )
{
	setAxisScale( xBottom, 0, size );
	setAxisScale( yLeft, 0, scale );

	ai_time = new QwtPlotCurve();
	ai_time->setRenderHint( QwtPlotItem::RenderAntialiased );
	ai_time->setPen( QPen( game_gui::get_instance()->graphics->get_QColor( pl_id ) ) );
	ai_time->attach( this );
	ai_time->setData( GraphData( data ) );

	meanY = new QwtPlotMarker();
	meanY->setLabelAlignment( Qt::AlignRight | Qt::AlignTop );
	meanY->setLabel( QwtText( "Mean time" ) );
	meanY->setLineStyle( QwtPlotMarker::HLine );
	meanY->setYValue( mean );
	meanY->attach( this );
};



time_graph_widget::~time_graph_widget()
{
	delete ai_time;
	delete meanY;
};



ai_statistics_widget::ai_statistics_widget( QWidget* parent, Qt::WFlags flags ): QWidget( parent, flags )
{
	this->setWindowTitle( "AI Tester - AI Statistics" );
	QVBoxLayout* layout = new QVBoxLayout( this );


	QLabel* title_l = new QLabel( this );
	title_l->setTextFormat( Qt::RichText );
	ostringstream sout;
	sout << "<b><font size=6>AI Statistics</font></b>";
	title_l->setText( sout.str().c_str() );
	title_l->setAlignment( Qt::AlignHCenter );
	sout.str( "" );

	layout->addWidget( title_l );

	layout->addSpacing( 20 );

	int total_size = -1;
	double max = -1e50;
	double temp_max;
	player* pl;
	for ( unsigned int i = 0; i < players::get_instance()->get_players_count(); ++i )
	{
		pl = players::get_instance()->get_player( i );
		if ( pl->get_type() == ai )
		{
			if ( ( int )pl->ai_algorithm->time_cons.size() > total_size )
				total_size = ( int )pl->ai_algorithm->time_cons.size();
			temp_max = get_max( &( pl->ai_algorithm->time_cons ) );
			if ( temp_max > max )
				max = temp_max;
		}
	}

	if ( total_size == -1 )
	{
		this->setLayout( layout );
		return;
	}

	time_graph_widget* tgw;
	for ( unsigned int i = 0; i < players::get_instance()->get_players_count(); ++i )
	{
		pl = players::get_instance()->get_player( i );
		if ( pl->get_type() == ai )
		{
			sout << pl->get_name() << " (player " << pl->get_id() << ") - " << pl->ai_algorithm->get_name();
			tgw = new time_graph_widget( &( pl->ai_algorithm->time_cons ), sout.str(), max, total_size, pl->get_id(), get_mean(  &( pl->ai_algorithm->time_cons ) ), this );
			layout->addWidget( tgw );
			sout.str( "" );
		}
	}

	this->setLayout( layout );
};



double ai_statistics_widget::get_max( vector< double >* data )
{
	double max = -1e50;
	for ( unsigned int i = 0; i < data->size(); ++i )
		if ( ( *data )[ i ] > max )
			max = ( *data )[ i ];
	return max;
};



double ai_statistics_widget::get_mean( vector< double >* data )
{
	double mean = 0;
	for ( unsigned int i = 0; i < data->size(); ++i )
		mean += ( *data )[ i ];
	mean = mean / data->size();
	return mean;
};